package com.zz.web.config;

import com.zz.common.coder.GzipCodec;
import com.zz.common.handle.TunnelProtocolDecode;
import com.zz.common.handle.TunnelProtocolEncode;
import com.zz.web.handle.WebUIChannelHandle;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebUIConfig {


    @Bean
    public Bootstrap getBootstrap() {
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(workerGroup);
        bootstrap.channel(NioSocketChannel.class);
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                // 压缩解压
                ch.pipeline().addLast(new GzipCodec());
                ch.pipeline().addLast(new TunnelProtocolDecode(Integer.MAX_VALUE, 0, 4, 0, 0));
                ch.pipeline().addLast(new TunnelProtocolEncode());
                // 请求分片处理
                ch.pipeline().addLast(new ChunkedWriteHandler());
                //ch.pipeline().addLast(new IdleCheckHandler(IdleCheckHandler.READ_IDLE_TIME, IdleCheckHandler.WRITE_IDLE_TIME - 10, 0));
                ch.pipeline().addLast(new WebUIChannelHandle());
            }
        });
        return bootstrap;
    }

}
